package cn.buyclss.backend.service.impl.shop;

import cn.buyclss.backend.entity.domain.User;
import cn.buyclss.backend.entity.domain.shop.GoodsSpecs;
import cn.buyclss.backend.entity.domain.shop.GoodsSpecsExample;
import cn.buyclss.backend.entity.domain.shop.OrderGoodsSpecs;
import cn.buyclss.backend.entity.domain.shop.OrderGoodsSpecsExample;
import cn.buyclss.backend.entity.po.shop.OrderDetailPO;
import cn.buyclss.backend.entity.support.RespResult;
import cn.buyclss.backend.mapper.shop.GoodsSpecsMapper;
import cn.buyclss.backend.mapper.shop.OrderGoodsSpecsMapper;
import cn.buyclss.backend.mapper.shop.OrderMapper;
import cn.buyclss.backend.entity.po.shop.OrderPO;
import cn.buyclss.backend.service.shop.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: Cecurio
 * @create: 2019-03-21 22:23
 **/
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderGoodsSpecsMapper orderGoodsSpecsMapper;

    @Autowired
    private GoodsSpecsMapper goodsSpecsMapper;


    @Override
    public RespResult getMyOrders(Integer pageIndex, Integer pageSize) {
        RespResult res = RespResult.successInstance();

        Subject subject = SecurityUtils.getSubject();
        Object obj = subject.getPrincipal();
        Integer uid = 0;
        if (obj instanceof User) {
            User user = (User) obj;
            uid = user.getId();
        } else {
            return RespResult.failureInstance("系统异常，稍后重试！");
        }

        // startPage(第几页, 多少条数据)
        PageHelper.startPage(pageIndex, pageSize);
        List<OrderPO> orderPOList = orderMapper.selectOrderWithCustomerAndShopByUserId(uid);
        // 用PageInfo对结果进行包装
        PageInfo<OrderPO> pageInfo = new PageInfo<OrderPO>(orderPOList);
        res.putValue("orders", pageInfo);


        return res;
    }

    @Override
    public RespResult getOrderDetailById(Integer orderId) {
        RespResult res = RespResult.successInstance();
        OrderDetailPO orderDetailPO = orderMapper.getOrderDetailById(orderId);

        Long totalMoney = 0L;
        // 通过order_id查询ns_order_goods_specs 获取规格 id 和数量
        OrderGoodsSpecsExample orderGoodsSpecsExample = new OrderGoodsSpecsExample();
        OrderGoodsSpecsExample.Criteria orderGoodsSpecsExampleCriteria = orderGoodsSpecsExample.createCriteria();
        orderGoodsSpecsExampleCriteria.andOrderIdEqualTo(orderId);
        List<OrderGoodsSpecs> orderGoodsSpecsList = orderGoodsSpecsMapper.selectByExample(orderGoodsSpecsExample);
        if (orderGoodsSpecsList == null || orderGoodsSpecsList.size() < 1) {
            return RespResult.failureInstance("此订单没有关联的商品，订单出错了！");
        }

        for (int i = 0; i < orderGoodsSpecsList.size(); i++) {
            OrderGoodsSpecs tempOrderGoodsSpecs = orderGoodsSpecsList.get(i);
            // 通过规格ID 查询价格
            GoodsSpecsExample example = new GoodsSpecsExample();
            GoodsSpecsExample.Criteria cr = example.createCriteria();
            cr.andIdEqualTo(tempOrderGoodsSpecs.getGoodsSpecsId());
            List<GoodsSpecs> goodsSpecsList = goodsSpecsMapper.selectByExample(example);
            if (goodsSpecsList == null || goodsSpecsList.size() < 1) {
                return RespResult.failureInstance("出错了，没有" + tempOrderGoodsSpecs.getGoodsSpecsId() + "类型的商品");
            }
            GoodsSpecs tempGoodsSpec = goodsSpecsList.get(0);
            // 计算总金额
            totalMoney = tempGoodsSpec.getPrice() * tempOrderGoodsSpecs.getGoodsCount();
        }
        orderDetailPO.setTotalMoney(totalMoney);
        res.putValue("order", orderDetailPO);
        return res;
    }
}
